<?php

/**
 * 数组处理
 */

namespace Services;

class Arrays
{

    /**
     * Access an array index, retrieving the value stored there if it exists or
     * a default if it does not. This function allows you to concisely access an
     * index which may or may not exist without raising a warning.
     *
     * @param   $array   array  Array to access.
     * @param   $key     mixed  Index to access in the array.
     * @param   $default mixed  Default value to return if the key is not
     *                   present in the array.
     *
     * @return  mixed  If $array[$key] exists, that value is returned. If not,
     *                  $default is returned without raising a warning.
     */
    public function value(array $array, $key, $default = null)
    {
        // isset() is a micro-optimization - it is fast but fails for null values.
        if(isset($array[$key])) {
            return $array[$key];
        }
        // Comparing $default is also a micro-optimization.
        if($default === null || array_key_exists($key, $array)) {
            return null;
        }
        return $default;
    }

    /**
     * 数据去重累加
     *
     * @param $array
     * @param string $reference_str  参照str
     * @param string $accumulation_str  累加str
     * @return mixed
     */
    public function heavyAccumulation($array , $reference_str, $accumulation_str)
    {
        $unset = array();

        for ($i = 0; $i < count($array); $i++){
            for ($j = $i+1; $j < count($array); $j++){
                if($array[$i][$reference_str] == $array[$j][$reference_str] && !in_array($j, $unset)){
                    $array[$i][$accumulation_str] += $array[$i][$accumulation_str];
                    $unset[] = $j;
                }
            }
        }

        foreach ($unset as $index => $value){
            unset($array[$value]);
        }

        return array_merge($array);
    }

    /**
     * 判断一位数组是否重复
     *
     * @param $array
     * @return bool
     */
    public function hasDuplicate($array)
    {
        $dupe_array = array();
        foreach ($array as $value) {
            if ( isset($dupe_array[$value]) ) {
                return true;
            } else {
                $dupe_array[$value] = true;
            }
        }

        return false;

        // TODO 效果不高 可以使用
        // return count($array) !== count(array_unique($array));
    }

    /**
     * 二维数组是否重复
     *
     * @param $array
     * @param $key
     * @return bool
     */
    public function hasTwoDimensionDuplicate($array, $key)
    {
        $dupe_array = array();
        foreach ($array as $value) {
            if ( !isset($value[$key]) ) {
                return false;
            }
            if ( isset($dupe_array[$value[$key]]) ) {
                return true;
            } else {
                $dupe_array[$value[$key]] = true;
            }
        }

        return false;
    }

    function array_remove($data, $key){
        if(!array_key_exists($key, $data)){
            return $data;
        }
        $keys = array_keys($data);
        $index = array_search($key, $keys);
        if($index !== FALSE){
            array_splice($data, $index, 1);
        }
        return $data;

    }

//    /**
//     * 数据层级化
//     *
//     * @param array $arr
//     * @param string $id
//     * @param string $pid
//     * @return mixed
//     */
//    public function findChildren(array $arr, $id = 'id', $pid = 'parent_id')
//    {
//        foreach($arr as $v) $data[$v[$id]] = $v;
//        foreach ($data as $k => $item){
//            if( $item[$pid]) {
//                $data[$item[$pid]]['child'][$item[$id]] = & $data[$k];
//            }
//        }
//
//        foreach ($data as $key=>$value) {
//            if ($value['parent_id']) {
//                unset($data[$key]);
//            }
//        }
//
//        return $this->fixKeys($data);
//    }
//
//    public function fixKeys($array)
//    {
//        $numberCheck = false;
//        foreach ($array as $k => $val) {
//            if (is_array($val)) {
//                $array[$k] = $this->fixKeys($val); //recurse
//                if (is_numeric($k)) $numberCheck = TRUE;
//            }
//        }
//
//        if ($numberCheck === true) {
//            return array_values($array);
//        } else {
//            return $array;
//        }
//    }

}
